<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/15
 * Time: 23:05
 */
namespace app\common\logic\export;





use app\common\logic\ExportService;
use app\common\model\custom\Niu;
use app\common\model\shop\Order;
use app\common\utils\Utils;
use think\Db;
use think\exception\ValidateException;

class CustomNiuExport
{
    //导出订单
    private static $new; //申请一个私有的静态成员变量来保存该类的唯一实例
    const TITLE_ARR = [
        'ID'         => [1, 1, 9],  //0为开始  1为结束
        '姓名'                 => [2,2, 12],
        '牧场名称'                       =>[3,3, 12],
        '电话'        => [4,4, 15],
        '牧场地址'                              => [5,5, 30] ,
        '品种和数量'                   =>[6,6, 30],
    ];
    const SUBMIT = [
        'author' =>'育牛',
        'subject' =>'育牛',
        'title' =>'牛场信息导出',
    ];
    private function __construct() {

    }


    public static function instance () { //声明一个静态公共方法，供外部获取唯一实例
        if (!(self::$new instanceof self)) {
            self::$new = new self;
        }
        return self::$new;
    }




    /**
     * 生成税率 报表
     */
    public function export($where = []) {
        $name = '订单导出';
        $titleArr = self::TITLE_ARR;
        $submit = self::SUBMIT;

        //list($sheetName, $sort, $line, $sheet) = [ $timeStr . '-表0', 1, 1, 0]; //初始化变量
        list($sheetName, $sort, $line, $sheet) = [$name, 1, 1, 0]; //初始化变量
        $excel = ExportService::instance()->setTitles($submit);

        //初始化
        $excel->createSheet($sheetName, $sheet)->setSheetHeader($line, $titleArr);
        $list = (new Niu())->with(['user'])->where($where)->order('createtime', 'desc')->select();


        foreach ($list as $k => $v) {
            $line ++;

            $value = [
                $v['id'],
                $v['real_name'],
                $v['name'],
                $v['phone'],
                $v['address'],
                $v['type_data']['str']
            ];
            $excel->insertValues($line, $value);
            $sort ++;
        }

        $excel->output('Xlsx', $name);
        return true;
    }


    private function __clone() {} //声明私有的克隆方法，防止对象被克隆

    public function __sleep() { //重写__sleep方法，将返回置空，防止序列化反序列化获得新的对象
        return [];
    }
}